home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Cafe 3
/
Visual Cafe 3.ISO
/
Vcafe
/
Main.bin
/
Platform.java
< prev
next >
Wrap
Text File
|
1998-10-14
|
17KB
|
678 lines
package com.symantec.itools.lang;
import java.io.IOException;
import com.symantec.itools.util.Properties;
/**
* The Platform class abstracts varous System properties to provide
* a consistant set of names. Different vendors provide different
* values for these System properties which makes it hard to do
* comparisons against them.
*
* The following properties are abstracted:
* - java.vendor
* - java.version
* - os.name
* - os.version
* - os.arch
*
* The concept of an OS family (eg: Windows NT, Window 95 and
* Windows 98 are all part of the Windows family) is also
* introduced.
*
* The abstraction is driven by a Symantec style Property file.
* This file is broken up into three main sections:
*
* - vm; abstracts the VM Info
* - os; abstracts the OS info
* - machine; abstracts the Architecture info
*
* Each section is further broken down by Java vendor. The
* reasoning behind choosing the Java vendor is that it makes
* for finer control over the information. If no vendor were
* used the number of conflicting values and thus the chance
* for error gets quite large. Trying to combine similar VMs
* (Symantec and Sun return the same values currently) makes
* the file too complicated.
*
* The Property file is /com/symantec/itools/lang/Platform.properties
* Any unknown values can easily be added to the property file
* without having to recompile any code.
*
* @author Symantec Internet Tools Division
* @version 1.0
* @since VCafe 3.0
* @see com.symantec.itools.util.Properties
*/
public class Platform
{
/**
* Tell if unknown information should be reported
* @since VCafe 3.0
*/
protected static boolean reportErrors;
/**
* The key from the property file; null if unknown
* @since VCafe 3.0
*/
protected static String javaVendor;
/**
* The key from the property file; null if unknown
* @since VCafe 3.0
*/
protected static String javaVersion;
/**
* The key from the property file; null if unknown.
* @since VCafe 3.0
*/
protected static String osName;
/**
* The key from the property file; null if unknown
* @since VCafe 3.0
*/
protected static String osVersion;
/**
* The key from the property file; null if unknown
* @since VCafe 3.0
*/
protected static String osType;
/**
* The key from the property file; null if unknown
* @since VCafe 3.0
*/
protected static String architecture;
/**
* Holds the info from the property file.
* @since VCafe 3.0
*/
protected static Properties properties;
/**
* Constant representing Apple.
* @since VCafe 3.0
* @see #javaVendor
* @see #isJavaVendor
*/
public static final String VENDOR_APPLE = "Apple";
/**
* Constant representing IBM.
* @since VCafe 3.0
* @see #javaVendor
* @see #isJavaVendor
*/
public static final String VENDOR_IBM = "IBM";
/**
* Constant representing Microsoft.
* @since VCafe 3.0
* @see #javaVendor
* @see #isJavaVendor
*/
public static final String VENDOR_MICROSOFT = "Microsoft";
/**
* Constant representing Netscape.
* @since VCafe 3.0
* @see #javaVendor
* @see #isJavaVendor
*/
public static final String VENDOR_NETSCAPE = "Netscape";
/**
* Constant representing Silicon Graphics.
* @since VCafe 3.0
* @see #javaVendor
* @see #isJavaVendor
*/
public static final String VENDOR_SGI = "SGI";
/**
* Constant representing Sun.
* @since VCafe 3.0
* @see #javaVendor
* @see #isJavaVendor
*/
public static final String VENDOR_SUN = "Sun";
/**
* Constant representing Symantec.
* @since VCafe 3.0
* @see #javaVendor
* @see #isJavaVendor
*/
public static final String VENDOR_SYMANTEC = "Symantec";
/**
* Constant representing Digital.
* @since VCafe 3.0
* @see #javaVendor
* @see #isJavaVendor
*/
public static final String VENDOR_DEC = "DEC";
/**
* Constant representing Hewlet Packard.
* @since VCafe 3.0
* @see #javaVendor
* @see #isJavaVendor
*/
public static final String VENDOR_HP = "HP";
/**
* Constant representing the Linux JDK port.
* @since VCafe 3.0
* @see #javaVendor
* @see #isJavaVendor
*/
public static final String VENDOR_LINUX_PORT = "LinuxPort";
/**
* @since VCafe 3.0
*/
public static final String JDK_1_1 = "1-1";
/**
* @since VCafe 3.0
*/
public static final String JDK_1_2 = "1-2";
/**
* @since VCafe 3.0
*/
public static final String OS_WINDOWS_NT = "WindowsNT";
/**
* @since VCafe 3.0
*/
public static final String OS_WINDOWS_95 = "Windows95";
/**
* @since VCafe 3.0
*/
public static final String OS_WINDOWS_98 = "Windows98";
/**
* @since VCafe 3.0
*/
public static final String OS_MACINTOSH = "Macintosh";
/**
* @since VCafe 3.0
*/
public static final String OS_SOLARIS = "Solaris";
/**
* @since VCafe 3.0
*/
public static final String OS_IRIX = "Irix";
/**
* @since VCafe 3.0
*/
public static final String OS_DIGITAL_UNIX = "DigitalUnix";
/**
* @since VCafe 3.0
*/
public static final String OS_AIX = "AIX";
/**
* @since VCafe 3.0
*/
public static final String OS_HPUX = "HPUX";
/**
* @since VCafe 3.0
*/
public static final String OS_LINUX = "Linux";
/**
* @since VCafe 3.0
*/
public static final String OS_TYPE_MAC_OS = "MacOS";
/**
* @since VCafe 3.0
*/
public static final String OS_TYPE_WINDOWS = "Windows";
/**
* @since VCafe 3.0
*/
public static final String OS_TYPE_UNIX = "Unix";
/**
* @since VCafe 3.0
*/
public static final String ARCHITECTURE_MIPS = "MIPS";
/**
* @since VCafe 3.0
*/
public static final String ARCHITECTURE_X86 = "x86";
/**
* @since VCafe 3.0
*/
public static final String ARCHITECTURE_ALPHA = "ALPHA";
/**
* @since VCafe 3.0
*/
public static final String ARCHITECTURE_SPARC = "SPARC";
/**
* @since VCafe 3.0
*/
public static final String ARCHITECTURE_PA_RISC = "PA-RISC";
static
{
try
{
properties = new Properties("/com/symantec/itools/lang/Platform.properties");
}
catch(Throwable ex)
{
ex.printStackTrace();
}
}
static
{
reportErrors = true;
}
static
{
try
{
Properties info;
info = properties.getProperties("vm");
if(info != null)
{
javaVendor = lookupJavaVendor(info);
if(javaVendor != null)
{
javaVersion = lookupJavaVersion(info);
info = properties.getProperties("os");
if(info != null)
{
osName = lookupOSName(info);
if(osName != null)
{
osVersion = lookupOSVersion(info);
osType = lookupOSType(info);
}
}
info = properties.getProperties("machine");
if(info != null)
{
architecture = lookupArchitecture(info);
}
}
}
}
catch(Throwable ex)
{
ex.printStackTrace();
}
fixupOSName();
if(reportErrors)
{
reportErrorFor("java.vendor", javaVendor, true);
reportErrorFor("java.version", javaVersion, true);
reportErrorFor("os.name", osName, true);
reportErrorFor("os.version", osVersion, true);
reportErrorFor("the OS type", osType, false);
reportErrorFor("os.arch", architecture, true);
}
}
/**
* @since VCafe 3.0
*/
protected Platform()
{
throw new IllegalInstantiationError(Platform.class);
}
/**
* @param vendor The representation of the Java vendor. This is the key in the properties file.
* @since VCafe 3.0
*/
public static boolean isJavaVendor(String vendor)
{
return (vendor.equals(javaVendor));
}
/**
* @param version The representation of the Java version. This is the key in the properties file.
* @since VCafe 3.0
*/
public static boolean isJavaVersion(String version)
{
return (version.equals(javaVersion));
}
/**
* @param type The representation of the OS family. This is the key in the properties file.
* @since VCafe 3.0
*/
public static boolean isOSType(String type)
{
return (type.equals(osType));
}
/**
* @param os The representation of the OS name. This is the key in the properties file.
* @since VCafe 3.0
*/
public static boolean isOSName(String os)
{
return (os.equals(osName));
}
/**
* @param arch The representation of the OS version. This is the key in the properties file.
* @since VCafe 3.0
*/
public static boolean isArchitecture(String arch)
{
return (arch.equals(architecture));
}
/**
* @param vmInfo The properties object that holds the "vm" part of the properties file.
* @since VCafe 3.0
*/
private static String lookupJavaVendor(Properties vmInfo)
{
final String prop = System.getProperty("java.vendor").trim();
Properties vendorInfo;
String[] vendorList;
vendorInfo = vmInfo.getProperties("vendors");
if(vendorInfo == null)
{
throw new RuntimeException("Need a better error - vendorInfo is null");
}
vendorList = vendorInfo.getStringList("vendors");
for(int i = 0; i < vendorList.length; i++)
{
String[] list;
list = vendorInfo.getStringList(vendorList[i]);
for(int j = 0; j < list.length; j++)
{
if(prop.equals(list[j]))
{
return (vendorList[i]);
}
}
}
return (null);
}
/**
* @param vmInfo The properties object that holds the "vm" part of the properties file.
* @since VCafe 3.0
*/
private static String lookupJavaVersion(Properties vmInfo)
{
final String prop = System.getProperty("java.version").trim();
Properties versionInfo;
String[] versionList;
versionInfo = vmInfo.getProperties("versions");
if(versionInfo == null)
{
throw new RuntimeException("Need a better error - versionInfo is null");
}
versionList = versionInfo.getStringList("versions");
for(int i = 0; i < versionList.length; i++)
{
String[] list;
list = versionInfo.getStringList(versionList[i] + '.' + javaVendor);
for(int j = 0; j < list.length; j++)
{
if(prop.equals(list[j]))
{
return (versionList[i]);
}
}
}
return (null);
}
/**
* @param osInfo The properties object that holds the "os" part of the properties file.
* @since VCafe 3.0
*/
private static String lookupOSName(Properties osInfo)
{
final String prop = System.getProperty("os.name").trim();
Properties nameInfo;
String[] nameList;
nameInfo = osInfo.getProperties("names");
if(nameInfo == null)
{
throw new RuntimeException("Need a better error - nameInfo is null");
}
nameList = nameInfo.getStringList("names");
for(int i = 0; i < nameList.length; i++)
{
String[] list;
list = nameInfo.getStringList(nameList[i] + '.' + javaVendor);
for(int j = 0; j < list.length; j++)
{
if(prop.equals(list[j]))
{
return (nameList[i]);
}
}
}
return (null);
}
/**
* @param osInfo The properties object that holds the "os" part of the properties file.
* @since VCafe 3.0
*/
private static String lookupOSVersion(Properties osInfo)
{
final String prop = System.getProperty("os.version").trim();
Properties versionInfo;
String[] versionList;
versionInfo = osInfo.getProperties("versions");
if(versionInfo == null)
{
throw new RuntimeException("Need a better error - versionInfo is null");
}
versionList = versionInfo.getStringList(osName);
for(int i = 0; i < versionList.length; i++)
{
String[] list;
list = versionInfo.getStringList(osName + '.' + versionList[i] + '.' + javaVendor);
for(int j = 0; j < list.length; j++)
{
if(prop.equals(list[j]))
{
return (versionList[i]);
}
}
}
return (null);
}
/**
* @param osInfo The properties object that holds the "os" part of the properties file.
* @since VCafe 3.0
*/
private static String lookupOSType(Properties osInfo)
{
Properties typeInfo;
String[] typeList;
typeInfo = osInfo.getProperties("types");
if(typeInfo == null)
{
throw new RuntimeException("Need a better error - typesInfo is null");
}
typeList = typeInfo.getStringList("types");
for(int i = 0; i < typeList.length; i++)
{
String[] list;
list = typeInfo.getStringList(typeList[i]);
for(int j = 0; j < list.length; j++)
{
if(osName.equals(list[j]))
{
return (typeList[i]);
}
}
}
return (null);
}
/**
* @param machineInfo The properties object that holds the "machine" part of the properties file.
* @since VCafe 3.0
*/
private static String lookupArchitecture(Properties machineInfo)
{
final String prop = System.getProperty("os.arch").trim();
String[] architectureList;
architectureList = machineInfo.getStringList("machines");
for(int i = 0; i < architectureList.length; i++)
{
String[] list;
list = machineInfo.getStringList(architectureList[i] + '.' + javaVendor);
for(int j = 0; j < list.length; j++)
{
if(prop.equals(list[j]))
{
return (architectureList[i]);
}
}
}
return (null);
}
/**
* @param key The key for the System property
* @param value The value from the property file; if null an error is reported
* @param showSysProp TODO
* @since VCafe 3.0
*/
private static void reportErrorFor(String key, String value, boolean showSysProp)
{
if(value == null)
{
System.out.print("Platform doesn't know about " + key);
if(showSysProp)
{
System.out.print(": " + System.getProperty(key));
}
System.out.println();
}
}
/**
* Gives the chance to modify the name of the OS once all of the
* information is known. For example Win98 reports an OS name of
* Win95 - but the version numbers differ. If the name were not
* fixed up the check for Win98 would have to be to look at the
* OS name and the OS version.
* @since VCafe 3.0
*/
private static void fixupOSName()
{
if(osName == null | osVersion == null)
{
return;
}
// handle the screwed up MS OS naming convention where Win98 reports as Win95!
if(osName.equals(OS_WINDOWS_95))
{
if(osVersion.equals("4.10"))
{
osName = OS_WINDOWS_98;
}
}
}
/**
* This exists for testing purposes only.
* @param argv Not used
* @since VCafe 3.0
*/
public static void main(String[] argv)
{
}
}